package com.manager;


import com.google.gson.Gson;
import com.selector.ModelSelector;
import com.service.ChatService;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class ChatManager {
    private final ChatService chatService;
    private final ModelSelector modelSelector;
    private final Gson gson;
    private static String fileName;
    public static Boolean isLoadHistory = false;

    public static String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public ChatManager(ChatService chatService, ModelSelector modelSelector, Gson gson) {
        this.chatService = chatService;
        this.modelSelector = modelSelector;
        this.gson = gson;
    }

    public void run() {
        fileName = null;
        Scanner scanner = new Scanner(System.in);
        System.out.print("是否加载之前的聊天记录(y/n): ");
        if (scanner.nextLine().equalsIgnoreCase("y")) {
            try {
                //读取聊天记录从resources/history目录下
                //获取文件列表后缀为Json的文件
                File[] files = new File("E:/develop/code/learnjava/ChatModels/src/main/resources/history/").listFiles((dir, name) -> name.endsWith(".json"));
                Map<String, String> filesMap = new HashMap<>();
                if (files != null && files.length > 0) {
                    for (File file : files) {
                        String fileName = file.getName();
                        try {
                            String fileContent = new String(java.nio.file.Files.readAllBytes(file.toPath()));
                            filesMap.put(fileName, fileContent);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    Integer i = 1;
                    String Content = "请选择聊天记录文件：";
                    List<String> fileCount = new ArrayList<>();
                    for (Map.Entry<String, String> entry : filesMap.entrySet()) {
                        String fileName = entry.getKey();
                        String fileContent = entry.getValue();
                        System.out.println(i + "." + fileName);
                        i++;
                        fileCount.add(fileName);
                    }
                    fileName = fileCount.get(scanner.nextInt() - 1);
                    if (fileName != null){
                        isLoadHistory = true;
                        chatService.loadChatHistory();
                    }
                }else {
                    System.out.println("没有聊天记录文件，开始新的对话。");
                    chatService.initializeNewSession(modelSelector.selectModel(), scanner);
                }
            } catch (IOException e) {
                System.out.println("加载聊天记录失败，开始新的对话。");
                chatService.initializeNewSession(modelSelector.selectModel(), scanner);
            }
        } else {
            chatService.initializeNewSession(modelSelector.selectModel(), scanner);
        }
        try {

            chatService.startChatLoop();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}